package loadvideo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/11.
 */
public class HttpUtilVideo {
    public static byte[] getJsonByPath(String path) throws IOException {
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        ByteArrayOutputStream baos = null;
        conn.setConnectTimeout(5000);
        if (conn.getResponseCode() == 200) {
            InputStream in = conn.getInputStream();
            int len = 0;
            byte[] buffer = new byte[1024 * 2];
            baos = new ByteArrayOutputStream();
            while ((len = in.read(buffer)) != -1) {

                baos.write(buffer, 0, len);
                baos.flush();
            }
            baos.close();
            in.close();
        }

        return baos.toByteArray();
    }
    public static List<Video> getMsgbyJson(String string) throws JSONException {
        ArrayList<Video> list = new ArrayList<>();
        JSONObject object = new JSONObject(string);
        JSONArray jsonArray = object.getJSONArray("list");
        for (int i=0;i<jsonArray.length();i++){
            JSONObject obj = jsonArray.getJSONObject(i);
            String logo = obj.getString("logo");
            String name = obj.getString("name");
            String highlight = obj.getString("highlight");
            String grade = obj.getString("grade");
            String actors = obj.getString("mobilePreview");
            Video video = new Video(logo,name,highlight,grade,actors);
            list.add(video);
        }
        return list;
    }
}
